package spzx.product.controller;

import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.IndexVo;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spzx.product.service.BrandService;
import spzx.product.service.CategoryService;
import spzx.product.service.ProductService;

import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    @GetMapping("item/{skuId}")
    public Result item(@PathVariable Long skuId){
        ProductItemVo productItemVo = productService.item(skuId);
        return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, ProductSkuDto productSkuDto){
        PageInfo pageInfo = productService.findByPage(pageNum, pageSize, productSkuDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }



    /**
     * 查询所有的品牌
     * @return list
     */
    @GetMapping("brand/findAll")
    public Result findAllBrand(){
        List<Brand> list = brandService.findAll();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有的一级商品分类，每个分类的下级一并查出，赋值到Children属性中，形成树形结构
     * @return
     */
    @GetMapping("category/findCategoryTree")
    public Result findCategoryTree(){
        //List<Category> oneCategoryList = categoryService.findCategoryTree(0L);//递归查询(多次sql)
        List<Category> oneCategoryList = categoryService.findCategoryTreeByOneSql();//一次sql(内存中不断筛选需要的数据)
        return Result.build(oneCategoryList,ResultCodeEnum.SUCCESS);
    }


    /**
     * 首页接口
     */
    @GetMapping("index")
    public Result index(){
        IndexVo indexVo = new IndexVo();

        List<Category> categoryList = categoryService.findOneCategoryList(0L);
        List<ProductSku> productSkuList = productService.findProductSkuList();
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);

        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }
}
